package com.oguzhan.blog.scheduloor;


public final class Scheduloor {

	private JobList jobList;
	
	public Scheduloor() {
		this.jobList = new JobList();
	}
	
	public synchronized void scheduleJob(Job job) {
		if (job != null) {
			this.scheduleJob(new Job[]{job});
		}
		else {
			throw new IllegalArgumentException("Cannot schedule a NULL job");
		}
	}

	public synchronized void scheduleJob(Job... jobs) {
		if (jobs != null && jobs.length > 0) {
			for (Job job : jobs) {
				switch (job.getStatus()) {
				case CANCELLED:
				case COMPLETED:
				case RUNNING:
				case SCHEDULED:
					throw new IllegalArgumentException("Cannot add "+job.getStatus()+" job(s) to job-list");
				}
			}
	
			jobList.add(jobs);
			jobList.arrange();
		}
		else {
			throw new IllegalArgumentException("Cannot add null or empty jobs array to job-list");
		}
	}
	
	public synchronized void cancelJob(Job job) {
		if (job != null) {
			switch (job.getStatus()) {
			case CANCELLED:
			case COMPLETED:
			case RUNNING:
			case READY:
				throw new IllegalArgumentException("Cannot cancel "+job.getStatus()+" job");
			}
		}
		
		job.cancel();
		jobList.remove(job);
		jobList.arrange();
	}
	
}
